﻿using IT_RoadBlock.Entities;
using IT_RoadBlock.Handlers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Input;

namespace IT_RoadBlock.Comands
{
    /// <summary>
    /// 列表通用命令
    /// </summary>
    public abstract class ListRelayCommand<T> : IWindowClose where T: BaseSelectEntity
    {

        private List<T> dataList;
        /// <summary>
        /// 主体数据
        /// </summary>
        public List<T> DataList
        {
            get => dataList; set
            {
                dataList = value;
                OnPropertyChanged(nameof(DataList));
            }
        }

        #region SearchCmd
        /// <summary>
        /// 搜索
        /// </summary>
        public ICommand SearchCmd
        {
            get
            {
                return new RelayCommand<T>(SearchCmdExecute, CanSearchCmdExecute);
            }
        }
        protected bool CanSearchCmdExecute()
        {
            return true;
        }

        public abstract void SearchCmdExecute(T obj);
        #endregion

        #region SearchCmd
        /// <summary>
        /// 新增or编辑
        /// </summary>
        public ICommand AddOrEditItemCmd
        {
            get
            {
                return new RelayCommand<T>(AddItemOrEditCmdExecute, CanAddItemOrEditCmdExecute);
            }
        }
        protected bool CanAddItemOrEditCmdExecute()
        {
            return true;
        }

        /// <summary>
        /// 打开编辑窗口
        /// </summary>
        public Action<T> OpenEditWindow { get; set; }

        public void AddItemOrEditCmdExecute(T obj)
        {
            if (obj is T it)
            {
                OpenEditWindow.Invoke(it);
            }
            else
            {
                OpenEditWindow.Invoke(default);
                SearchCmdExecute(obj);
            }

        }
        #endregion

        #region 全选/反选==选中数据
        public ICommand SelectDataCommand
        {
            get
            {
                return new RelayCommand<object>(SelectDataCommandExecute, () => true);
            }
        }

        private void SelectDataCommandExecute(object check)
        {
            if (dataList?.Any() == true)
            {
                foreach (var item in dataList)
                {
                    item._DataSelected = !item._DataSelected;
                }
            }
        }
        #endregion



        #region RemoveItemsCmd
        /// <summary>
        /// 新增
        /// </summary>
        public ICommand RemoveItemsCmd
        {
            get
            {
                return new RelayCommand<T>(RemoveItemsCmdExecute, CanRemoveItemsCmdExecute);
            }
        }



        private bool CanRemoveItemsCmdExecute()
        {
            return true;
        }

        public abstract void RemoveItemsCmdExecute(T obj);
        #endregion

        private int pageCount = 0;
        private int pageSize = 10;
        private int pageNumber = 1;
        public int PageCount
        {
            get => pageCount; set
            {
                pageCount = value;
                OnPropertyChanged(nameof(PageCount));
            }
        }
        public int PageNumber
        {
            get => pageNumber; set
            {
                pageNumber = value;
                OnPropertyChanged(nameof(PageNumber));
                SearchCmdExecute(null);
            }
        }

        public int PageSize
        {
            get => pageSize; set
            {
                pageSize = value;
                OnPropertyChanged(nameof(PageSize));
            }
        }
    }
}
